package com.example.gustavo.hello.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;

public class NetWorkReceiver extends BroadcastReceiver {
    public NetWorkReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String s = intent.toString();
        String flags = intent.getAction();
        System.out.println("flags = " + flags);
        System.out.println("s = " + s);
        Bundle bundle = intent.getExtras();
        for (String s1 : bundle.keySet()) {
            System.out.println(s1 + " = " + bundle.get(s1).toString());
        }


        int netType = -1;
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
        boolean available = false;
        if (networkInfo != null) {
            available = networkInfo.isAvailable();
        }

        if(available){
            Toast.makeText(context, "net true", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context, "net false", Toast.LENGTH_SHORT).show();
        }
        /*int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
                netType = 3;
            } else {
                netType = 2;
            }
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = 1;
        }
        return netType;*/

    }
}
